package com.ztgf.order.api.cms;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.common.utils.ExcelExport2;
import com.ztgf.order.models.bo.OrderGMVStatisticsBO;
import com.ztgf.order.models.bo.OrderShopStatisticsBO;
import com.ztgf.order.models.bo.OrderStatisticsBO;
import com.ztgf.order.models.service.IOrderGMVStatisticsService;
import com.ztgf.order.models.service.IOrderSalesStatisticsService;
import com.ztgf.order.models.vo.OrderGMVStatisticsVO;
import com.ztgf.order.models.vo.OrderSalesCategoryNewVO;
import com.ztgf.order.models.vo.OrderSalesCategoryShopVO;
import com.ztgf.order.models.vo.OrderSalesCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/*
 * @author: chenyu.wang
 * @date: 2020-08-10
 * @description:后台管理系统GMV统计控制器
 */
@RestController
@RequestMapping("/cms/orderGMVStatisticsController")
@Api(value = "OrderGMVStatisticsController", tags = "后台管理系统GMV统计接口")
public class OrderGMVStatisticsController {

    @DubboReference
    private IOrderGMVStatisticsService iOrderGMVStatisticsService;
    @DubboReference
    private IOrderSalesStatisticsService iOrderSalesStatisticsService;

    @PostMapping(value = "/queryOrderStateAmount")
    @ApiOperation(value = "GMV各状态订单统计", notes = "GMV各状态订单统计")
    public Result queryCommodityTotal(OrderGMVStatisticsBO bo) {
        OrderGMVStatisticsVO vo = iOrderGMVStatisticsService.queryOrderStateAmount(bo);
        return new Result<>(vo);
    }

    @PostMapping(value = "/queryOrderSalesCategoryGmv")
    @ApiOperation(value = "各类目整体GMV统计", notes = "各类目整体GMV统计")
    public Result queryOrderSalesCategory(OrderStatisticsBO bo) {
//        Page<OrderSalesCategoryVO> page = iOrderGMVStatisticsService.queryOrderSalesCategory(bo);
        Page<OrderSalesCategoryNewVO> page = iOrderGMVStatisticsService.queryNewGmvOrderCount(bo);
        return new Result<>(page);
    }

    @PostMapping(value = "/queryOrderVariousShopItemsGmv")
    @ApiOperation(value = "各类目店铺GMV统计", notes = "各类目店铺GMV统计")
    public Result queryOrderVariousShopItems(OrderShopStatisticsBO bo) {
        Page<OrderSalesCategoryShopVO> page = iOrderGMVStatisticsService.queryOrderVariousShopItems(bo);
        return new Result<>(page);
    }

    @GetMapping(value = "/exportOrderSalesCategoryGmv")
    @ApiOperation(value = "各类目整体GMV统计导出", notes = "各类目整体GMV统计导出")
    public void exportOrderSalesCategoryGmv(HttpServletResponse response, String startTime, String endTime) {
        //创建一个数组用于设置表头
        String[] attributeNames = new String[]{"编号", "类目名称", "订单销量"};
        //查询数据
        OrderStatisticsBO bo = new OrderStatisticsBO();
        bo.setPageNo(1);
        bo.setPageSize(1000);
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            bo.setStartTime(startTime);
            bo.setEndTime(endTime);
        }
        Page<OrderSalesCategoryNewVO> page = iOrderGMVStatisticsService.queryNewGmvOrderCount(bo);
        List<OrderSalesCategoryNewVO> list = page.getRecords();
        if(CustomizeUtil.listIsNull(list)){
            OrderSalesCategoryNewVO orderSalesCategoryNewVO = new OrderSalesCategoryNewVO();
            orderSalesCategoryNewVO.setCategoryName("暂无信息");
            orderSalesCategoryNewVO.setOrderPrice(new BigDecimal("0.00"));
            list.add(orderSalesCategoryNewVO);
        }
        ExcelExport2.export(response, list, attributeNames, "_各类目整体GMV");
    }

    @GetMapping(value = "/exportOrderSalesVariousShopItemsGmv")
    @ApiOperation(value = "各类目店铺GMV统计导出", notes = "各类目店铺GMV统计导出")
    public void exportOrderSalesVariousShopItemsGmv(HttpServletResponse response, String startTime, String endTime) {
        //创建一个数组用于设置表头
        String[] attributeNames = new String[]{"编号", "类目编号", "店铺名称", "类目名称", "销售额"};
        //查询数据
        OrderShopStatisticsBO bo = new OrderShopStatisticsBO();
        bo.setPageNo(1);
        bo.setPageSize(1000);
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            bo.setStartTime(startTime);
            bo.setEndTime(endTime);
        }
        Page<OrderSalesCategoryShopVO> page = iOrderGMVStatisticsService.queryOrderVariousShopItems(bo);
        List<OrderSalesCategoryShopVO> list = page.getRecords();
        ExcelExport2.export(response, list, attributeNames, "_各类目店铺GMV");
    }
}
